package net.easyunion.dataservice.common.data;

import java.sql.Connection;
import java.sql.SQLException;

import net.easyunion.dataservice.vo.JdbcTemplateVo;

import com.mchange.v2.c3p0.DriverManagerDataSource;

/**
 * 
 * @author lipenghui
 * 
 * @version 创建时间：2013年11月11日 下午2:36:06
 */
public class DataSourceManager {

	
	JdbcTemplateVo dsVo;

	public DataSourceManager(JdbcTemplateVo dsVo) {
		this.dsVo = dsVo;
	}

	/**
	 * 获取数据库连接对象
	 * @return
	 */
	public Connection getConn() {
		DriverManagerDataSource ds = new DriverManagerDataSource();
		ds.setDriverClass(dsVo.getJdbcClassName());
		ds.setJdbcUrl(dsVo.getJdbcUrl());
		ds.setUser(dsVo.getUsername());
		ds.setPassword(dsVo.getPassword());
		Connection conn = null;
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return conn;
	}

	/**
	 * 关闭数据库连接
	 * @param conn
	 * @return
	 */
	public int closeConn(Connection conn) {
		try {
			if (null != conn && !conn.isClosed()) {
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 2;
		}
		return 1;
	}

}
